import $ from 'jquery';

export default {
  rootApi:'http://localhost:4000/',
  _fetch(){
    /*let myRequest = 'http://localhost:4000/order/create';
      let myInit = {
        method: 'POST',
        mode: 'cors',
        cache: false,
        dataType: 'json',
        body: JSON.stringify(data),
      };
      fetch(myRequest, myInit).then((response) => {
        return response.json();
      }).then((json) => {
        console.log('1',json);
        router.push("/payment/" + json.data.insertId );
      }).catch((err)=>{
        console.log('2',err);
      });*/
  },
  _ajax(config) {
    $.ajax({
      url: this.rootApi + config.url,
      type: config.type,
      dataType: 'json',
      data: config.data,
      cache: false,
      xhrFields: {
        withCredentials: config.cookie||false
      },
      success: (d) => {
        if(d.error_code==0){
          $.isFunction(config.success) && config.success(d);
        }else{
          $.isFunction(config.fail) && config.fail(d);
        }
      },
      error: function (xhr, status, err) {
        $.isFunction(config.error) && config.error();
      }
    });
  },

  getProductList(f) {
    let {data,success,fail,error} = f;
    this._ajax({
      url: 'product',
      data: data,
      type:'get',
      success,
      fail,
      error
    });
  },
  getOrderList(f) {
    let {data,success,fail,error} = f;
    this._ajax({
      url: 'order/list',
      data: data,
      type:'get',
      success,
      fail,
      error
    });
  },
  getOrderDetail(f) {
    let {data,success,fail,error} = f;
    this._ajax({
      url: 'order/detail',
      data: data,
      type:'get',
      success,
      fail,
      error
    });
  },
  createOrder(f) {
    let {data,success,fail,error} = f;
    this._ajax({
      url: 'order/create',
      data: data,
      type:'post',
      success,
      fail,
      error
    });
  },
  login(f) {
    let {data,success,fail,error} = f;
    this._ajax({
      url: 'user/login',
      data: data,
      type:'post',
      cookie: true,
      success,
      fail,
      error
    });
  },
  isLogin(f) {
    let {data,success,fail,error} = f;
    this._ajax({
      url: 'user/islogin',
      data: data,
      type:'GET',
      cookie: true,
      success,
      fail,
      error
    });
  },
  logout(f) {
    let {data,success,fail,error} = f;
    this._ajax({
      url: 'user/logout',
      data: data,
      type:'post',
      cookie: true,
      success,
      fail,
      error
    });
  },




}
